package liujia.bt.display
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filters.BitmapFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import liujia.bt.display.base.BtAnim;
	import liujia.bt.display.base.BtAnimLite;
	import liujia.bt.display.base.BtAnimMax;
	import liujia.bt.display.base.BtCanvas;
	import liujia.bt.display.base.BtCanvasChild;
	import liujia.bt.display.core.BtAnalyse;
	import liujia.bt.display.core.BtFrame;
	import liujia.bt.display.core.BtRun;
	import liujia.bt.display.interf.IBtDisplayObject;
	import liujia.bt.display.interf.IBtMovieClip;
	import liujia.bt.util.ArrayUtils;
	import liujia.bt.util.DisplayObjectUtil;
	
	/**
	 * BtDisplay 所有的BtDisplay的显示对象都从这里获取、销毁
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtDisplay.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtDisplay
	{
		/**
		 *注册点类型：左上角(0,0)点
		 */		
		public static const REGISTER_POINT_00:int = 0;
		
		/**
		 *注册点类型：跟MovieClip一致
		 */	
		public static const REGISTER_POINT_MC:int = 1;
		
		/**
		 *注册点类型：中心点
		 */	
		public static const REGISTER_POINT_CENTER:int = 2;
		
		/**
		 *背景透明
		 */	
		public static var transparent:Boolean = true;
		
		/**
		 *初始化，请务必在开始的时候初始化一次,否则位图动画将停止播放 
		 * @param $stage
		 * 
		 */		
		public static function init($stage:Stage):void {
			BtRun.init($stage);
		}
		
		/**
		 *把MovieClip转成BtAnimLite
		 * @param mc 源MovieClip
		 * @param registerPointType 注册点类型
		 * @return 转成的BtAnimation
		 * 
		 */		
		public static function getBtAnimLiteByMC(mc:MovieClip, registerPointType:int=1):BtAnimLite {
			var btFrameVec:Vector.<BtFrame> = BtAnalyse.mcToBitmap(mc);
			var btDisplayObject:BtAnimLite = new BtAnimLite(btFrameVec[0].bitmapData);
			btDisplayObject = greateBtMovieClip(btDisplayObject, btFrameVec, registerPointType) as BtAnimLite;
			
			return btDisplayObject;
		}
		
		/**
		 *把MovieClip转成BtAnim
		 * @param mc 源MovieClip
		 * @param registerPointType 注册点类型
		 * @return 转成的BtAnimationX
		 * 
		 */
		public static function getBtAnimByMC(mc:MovieClip, registerPointType:int=1):BtAnim {
			var btFrameVec:Vector.<BtFrame> = BtAnalyse.mcToBitmap(mc);
			var btDisplayObject:BtAnim = new BtAnim(btFrameVec[0].bitmapData);
			btDisplayObject = greateBtMovieClip(btDisplayObject, btFrameVec, registerPointType) as BtAnim;
			return btDisplayObject;
		}
		
		/**
		 *把MovieClip转成BtAnimMax
		 * @param mc 源MovieClip
		 * @param registerPointType 注册点类型
		 * @return 转成的BtAnimationX
		 * 
		 */
		public static function getBtAnimMaxByMC(mc:MovieClip, registerPointType:int=1):BtAnimMax {
			var btFrameVec:Vector.<BtFrame> = BtAnalyse.mcToBitmap(mc);
			var btDisplayObject:BtAnimMax = new BtAnimMax(btFrameVec[0].bitmapData);
			btDisplayObject.mc = mc;
			btDisplayObject = greateBtMovieClip(btDisplayObject, btFrameVec, registerPointType) as BtAnimMax;
			return btDisplayObject;
		}
		
//		/**
//		 *把MovieClip转成BtCanvasChild 
//		 * @param mc 源MovieClip
//		 * @param registerPointType 注册点类型
//		 * @return 
//		 * 
//		 */
//		public static function getBtCanvasChildByMC(mc:MovieClip, registerPointType:int=1):BtCanvasChild {
//			var btFrameVec:Vector.<BtFrame> = BtAnalyse.mcToBitmap(mc);
//			var btDisplayObject:BtCanvasChild = new BtCanvasChild(btFrameVec[0].bitmapData);
//			btDisplayObject = greateBtMovieClip(btDisplayObject, btFrameVec, registerPointType) as BtCanvasChild;
//			
//			return btDisplayObject;
//		}
		
		/**
		 *克隆，共享bitmapData 
		 * @param $btDisplayObject 源btDisplayObject
		 * @param $ClassType 要克隆出来的类型 可传入值 BtAnimation\BtAnimationX, 默认=null则与源btDisplayObject的类型相同
		 * @return 
		 * 
		 */		
		public static function clone($btDisplayObject:IBtMovieClip, $ClassType:Class=null):* {
			var btFrameVec:Vector.<BtFrame> = $btDisplayObject.btFrameVec.concat();
			
			var btDisplayObject:IBtMovieClip = null;
			if ($ClassType) {
				if ($ClassType == BtAnimLite) {
					btDisplayObject = new BtAnimLite(btFrameVec[0].bitmapData);
				} else if ($ClassType == BtAnim) {
					btDisplayObject = new BtAnim(btFrameVec[0].bitmapData);
				} else if ($ClassType == BtAnimMax) {
					btDisplayObject = new BtAnimMax(btFrameVec[0].bitmapData);
				} else if ($ClassType == BtCanvasChild) {
					btDisplayObject = new BtCanvasChild(btFrameVec[0].bitmapData);
				} else {
					trace("[BtDisplay] clone() " + $ClassType + "不是可克隆的类！");
					return null;
				}
			} else {
				if ($btDisplayObject is BtAnimLite) {
					btDisplayObject = new BtAnimLite(btFrameVec[0].bitmapData);
				} else if ($btDisplayObject is BtAnim) {
					btDisplayObject = new BtAnim(btFrameVec[0].bitmapData);
				} else if ($btDisplayObject is BtAnimMax) {
					btDisplayObject = new BtAnimMax(btFrameVec[0].bitmapData);
				} else if ($btDisplayObject is BtCanvasChild) {
					btDisplayObject = new BtCanvasChild(btFrameVec[0].bitmapData);
				} else {
					trace("[BtDisplay] clone() " + $btDisplayObject + "不是可克隆的类！");
					return null;
				}
			}
			
			btDisplayObject = greateBtMovieClip(btDisplayObject, btFrameVec, $btDisplayObject.registerPointType);
			
			return btDisplayObject;
		}
		
		/**
		 *销毁btDisplayObject，移除显示列表，移除所有监听，停止播放，清空所有数据，但共享bitmapdata不释放
		 * @param $btDisplayObject 要销毁的btDisplayObject 
		 * 
		 */		
		public static function destroy($btDisplayObject:IBtDisplayObject):void {
			DisplayObjectUtil.removeDisplayObject($btDisplayObject as DisplayObject);
			$btDisplayObject.clearListeners();
			
			if ($btDisplayObject is IBtMovieClip) {
				($btDisplayObject as IBtMovieClip).stop();
				($btDisplayObject as IBtMovieClip).totalFrames = 0;
				($btDisplayObject as IBtMovieClip).btFrameVec.length = 0;
				($btDisplayObject as IBtMovieClip).btFrameVec = null;
			}
			$btDisplayObject.bitmapData = null;
			$btDisplayObject.currentBtFrame = null;
			$btDisplayObject.rect = null;
		}
		
		/**
		 *完全销毁，使用BitmapData.dispose()释放共享bitmapdata
		 * @param $btDisplayObject 要销毁的btDisplayObject
		 * 
		 */		
		public static function dispose($btDisplayObject:IBtMovieClip):void {
			var btFrameVec:Vector.<BtFrame> = $btDisplayObject.btFrameVec;
			for each (var btFrame:BtFrame in btFrameVec) {
				btFrame.bitmapData && btFrame.bitmapData.dispose();
				btFrame.bitmapData = null;
				btFrame.rect = null;
			}
			destroy($btDisplayObject);
		}
		
		
		
		
//////////////////////////////////////编辑帧//////////////////////////////////////////
		
		/**
		 *给位图动画增加btFrame帧 
		 * @param btFrame 要增加的帧
		 * @param btMovieClip 位图动画
		 * @param frame 加到的位置，默认-1即加至最后一帧
		 * @return totalFrames 执行之后总共的帧数
		 * 
		 */		
		public static function addBtFrame(btFrame:BtFrame, btMovieClip:IBtMovieClip, frame:int=-1):int {
			if (frame == -1) {
				btMovieClip.btFrameVec.push(btFrame);
			} else {
				if (frame < 1 || frame > btMovieClip.btFrameVec.length) {
					trace("[BtDisplay] addBtFrame() frame=" + frame + "超出范围!");
				} else {
					btMovieClip.btFrameVec.splice(frame-1, 0, btFrame);
				}
			}
			
			btMovieClip.totalFrames = btMovieClip.btFrameVec.length;
			return btMovieClip.totalFrames;
		}
		
		/**
		 *给位图动画移除btFrame帧  
		 * @param btMovieClip 要移除的帧
		 * @param frame 要移除的位置，默认-1即移除最后一帧
		 * @return 已移除的btFrame帧
		 * 
		 */		
		public static function removeBtFrame(btMovieClip:IBtMovieClip, frame:int=-1):BtFrame {
			var result:BtFrame = null;
			if (frame == -1) {
				result = btMovieClip.btFrameVec.pop();
			} else {
				if (frame < 1 || frame > btMovieClip.btFrameVec.length) {
					trace("[BtDisplay] removeBtFrame() frame=" + frame + "超出范围!");
				} else {
					result = btMovieClip.btFrameVec.splice(frame-1, 1)[0];
				}
			}
			
			btMovieClip.totalFrames = btMovieClip.btFrameVec.length;
			return result;
		}
		
		/**
		 *给位图动画增加镜像位图帧 
		 * @param btMovieClip 源btMovieClip（包括BtAnimation\BtAnimationX）
		 * @param frameArr 要翻转的位图帧序号组成的数组
		 * @return 增加镜像位图帧后的BtAnimation\BtAnimationX
		 * 
		 */		
		public static function addMirrorFrames(btMovieClip:IBtMovieClip, frameArr:Array):int {
			if (checkFrameArr(btMovieClip, frameArr)) {
				var matrix:Matrix = new Matrix(-1, 0, 0, 1, 0, 0);
				for each (var frame:int in frameArr) {
					var btFrame:BtFrame = btMovieClip.btFrameVec[frame];
					var addData:BitmapData = btFrame.bitmapData;
					matrix.tx = btFrame.bitmapData.width;
					var rect:Rectangle = btFrame.rect.clone();
					rect.x = -btFrame.rect.width - btFrame.rect.x;
					var bitmapData:BitmapData = new BitmapData(addData.width, addData.height, true, 0x00ff00);
					bitmapData.draw(addData, matrix);
					
					btMovieClip.btFrameVec.push(new BtFrame(bitmapData, rect));
				}
				btMovieClip.totalFrames = btMovieClip.btFrameVec.length;
			} 
			return btMovieClip.totalFrames;
		}
		
		/**
		 *深克隆BtFrame，并在克隆的过程中进行缩放和旋转, 不共享bitmapData
		 * @param sourceBtFrame 源BtFrame
		 * @param scaleX 水平缩放
		 * @param scaleY 垂直缩放
		 * @param rotation 角度
		 * @return 克隆出来的BtFrame
		 * 
		 */		
		public static function deepCloneBtFrame(btFrame:BtFrame, scaleX:Number=1, scaleY:Number=1, rotation:Number=0):BtFrame {
			return BtAnalyse.deepCloneBtFrame(btFrame, scaleX, scaleY, rotation);
		}
		
		/**
		 *检查祯索引
		 * @param btMovieClip 源btMovieClip（包括BtAnimation\BtAnimationX）
		 * @param frameArr 要翻转的位图帧序号组成的数组
		 * @return 是否可以进行水平翻转
		 * 
		 */		
		public static function checkFrameArr(btMovieClip:IBtMovieClip, frameArr:Array):Boolean {
			var resault:Boolean = true;
			for each (var frame:int in frameArr) {
				if (frame < 1 || frame > btMovieClip.totalFrames) {
					trace("[BtDisplay] checkFrameArr() frameArr的祯索引超出范围!");
					resault = false;
					break;
				}
			}
			return resault;
		}
		
		
		
//////////////////////////////////////私有方法//////////////////////////////////////////
		
		/**
		 *把 btFrameVec 组装成一个btDisplayObject
		 * @param btDisplayObject 需要组装的btDisplayObject
		 * @param btFrameVec 所用的位图帧
		 * @param registerPointType 注册点类型
		 * @return 组装后的可使用的btDisplayObject
		 * 
		 */		
		private static function greateBtMovieClip(btDisplayObject:IBtMovieClip, btFrameVec:Vector.<BtFrame>, registerPointType:int):IBtMovieClip {
			btDisplayObject.btFrameVec = btFrameVec;
			btDisplayObject.currentBtFrame = btFrameVec[0];
			btDisplayObject.totalFrames = btFrameVec.length;
			btDisplayObject.registerPointType = registerPointType;
			btDisplayObject.rect = btDisplayObject.currentBtFrame.rect;
			btDisplayObject.play();
			
			return btDisplayObject;
		}
		
	}
}
